package com.zen.payclient;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.alipay.sdk.app.PayTask;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;

public class MainActivity extends Activity {

    public static final String URL_CREATE_ORDER = "http://192.168.2.113:8080/pay/createOrder";
    public static final String URL_QUERY_RESULT = "http://192.168.2.113:8080/pay/queryPayResult";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.pay).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                HttpUtils.doGet(URL_CREATE_ORDER, new HttpUtils.IHttpRequestCallback() {
                    @Override
                    public void onResult(boolean success, String result) {
                        LOG.d("success: " + success + " result: " + result);
                        if (success) {
                            try {
                                JSONObject jo = new JSONObject(result);
                                int code = jo.optInt("code");
                                if (code == HttpUtils.HTTP_RESP_SUCCESS) {
                                    String orderInfo = jo.optString("data");
                                    alipay(orderInfo);
                                } else {
                                    String msg = jo.optString("msg");
                                    Toast.makeText(MainActivity.this, "" + msg, Toast.LENGTH_SHORT).show();
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });


            }
        });
    }

    private void alipay(final String orderInfo) {

        Runnable payRunnable = new Runnable() {

            @Override
            public void run() {
                PayTask alipay = new PayTask(MainActivity.this);
                final Map<String, String> result = alipay.payV2(orderInfo, true);
                LOG.d("result:- --- - " + result);
                HttpUtils.sHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        PayResult payResult = new PayResult(result);
                        /**
                         对于支付结果，请商户依赖服务端的异步通知结果。同步通知结果，仅作为支付结束的通知。
                         */
                        String resultInfo = payResult.getResult();// 同步返回需要验证的信息
                        String resultStatus = payResult.getResultStatus();
                        // 判断resultStatus 为9000则代表支付成功
                        if (TextUtils.equals(resultStatus, "9000")) {
                            // 该笔订单是否真实支付成功，需要依赖服务端的异步通知。
                            Toast.makeText(MainActivity.this, "支付成功", Toast.LENGTH_SHORT).show();
                            queryServerResult(resultInfo);
                        } else {
                            // 该笔订单真实的支付结果，需要依赖服务端的异步通知。
                            Toast.makeText(MainActivity.this, "支付失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        };
        // 必须异步调用
        Thread payThread = new Thread(payRunnable);
        payThread.start();
    }


    /**
     * 调用后台接口，查询订单信息
     *
     * @param info
     */
    private void queryServerResult(String info) {
        try {
            JSONObject jo = new JSONObject(info);
            JSONObject resp = jo.optJSONObject("alipay_trade_app_pay_response");
            if (resp != null) {
                String orderid = resp.optString("out_trade_no");
                String str = URL_QUERY_RESULT + "?orderId=" + orderid;
                HttpUtils.doGet(str, new HttpUtils.IHttpRequestCallback() {
                    @Override
                    public void onResult(boolean success, String result) {
                        LOG.d("queryServerResult : " + success + " " + result);
                    }
                });
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

}
